﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using HeapSpecSupport;
using SimpleIR;

namespace Evaluator.EvalState
{
	internal static class BuiltinSupport
	{
		/// <summary>
		/// Do the specialized initialization of the object class
		/// </summary>
		internal static void BIInitializeObject(LocalFrame cfstate, List<ELocation> memory, ulong ctime, long line)
		{
			EPointer op = cfstate.EvalStackPopWVal().PointerValue;
			EObject obj = LocalFrame.ResolvePointerTarget(op, memory);
			obj.SimulateObjectConstructor(ctime, line);
		}

		/// <summary>
		/// Do the specialized initialization for a keyvaluepair
		/// </summary>
		internal static void BIInitializeKeyValuePair(IRKeyValuePairType alloctype, LocalFrame cfstate, List<ELocation> memory, ulong ctime, long line)
		{
			EvalStackEntry val = cfstate.EvalStackPopWVal();
			EvalStackEntry key = cfstate.EvalStackPopWVal();

			//push 2 copies of the ref to the struct onto the stack for storing refs through
			cfstate.SimulateDup();
			cfstate.SimulateDup();

			//store the key
			cfstate.EvalStackPush(key);
			cfstate.SimulateDeRefIndirectWrite(memory, ctime, line);

			//store the value
			cfstate.EvalStackPush(val);
			cfstate.SimulateDeRefIndirectWrite(memory, ctime, line);

			cfstate.SimulateUnBox(alloctype, -1, ctime, line);
		}
	}
}
